package com.sheng.whyclass.person.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.StudentRegisterEvent;

import de.greenrobot.event.EventBus;

public class StudentRegisterActivity extends Activity implements View.OnClickListener {

    private TextView title;
    private EditText studentName;
    private EditText studentPass;
    private EditText studentClass;
    private EditText studentDepartment;
    private Button studentRegisterBtn;

    private String student_name;
    private String student_pass;
    private String student_class;
    private String student_department;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_register);
        EventBus.getDefault().register(this);
        initView();

    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        studentName = (EditText) findViewById(R.id.student_name);
        studentPass = (EditText) findViewById(R.id.student_pass);
        studentClass = (EditText) findViewById(R.id.student_class);
        studentDepartment = (EditText) findViewById(R.id.student_department);
        studentRegisterBtn = (Button) findViewById(R.id.student_register_btn);
        studentRegisterBtn.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.student_register_btn:
                student_name = studentName.getText().toString().trim();
                student_pass = studentPass.getText().toString().trim();
                student_class = studentClass.getText().toString().trim();
                student_department = studentDepartment.getText().toString().trim();

                if (TextUtils.isEmpty(student_name) || TextUtils.isEmpty(student_pass)||TextUtils.isEmpty(student_class)||TextUtils.isEmpty(student_department)) {
                    CustomToast.showLongToast(getString(R.string.para_null));
                    return;
                }
                register_student(student_name,student_pass,student_class,student_department);
                break;
        }
    }

    private void register_student(String student_name, String student_pass, String student_class, String student_department) {
        StringRequest request = HttpProcessManager.getInstance().registerStudent(Constant.HOST_STUDENT_REGISTER, student_name, student_pass,student_class,student_department);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);


    }

    public void onEventMainThread(StudentRegisterEvent event) {


        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            if(code==Constant.CODE_SUCCESS){
                startActivity(new Intent(StudentRegisterActivity.this, StudentLoginActivity.class));
                finish();
            }
            CustomToast.showToast(event.info);
        }

    }
}
